package com.example.common.i18n.util;


import cn.hutool.core.util.StrUtil;
import com.example.common.base.exception.CheckedException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author zxj
 * @date 2018/11/16 10:25
 */
public class AssertUtils {

	private static Validator validator;

	static {
		validator = Validation.buildDefaultValidatorFactory().getValidator();
	}

	/**
	 * 校验对象
	 * @param object        待校验对象
	 * @param groups        待校验的组
	 * @throws CheckedException  校验不通过，则报RRException异常
	 */
	public static void validateEntity(Object object, Class<?>... groups)
		throws CheckedException {
		Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
		if (!constraintViolations.isEmpty()) {
			StringBuilder msg = new StringBuilder();
			for(ConstraintViolation<Object> constraint:  constraintViolations){
				msg.append(constraint.getMessage()).append("<br>");
			}
			throw new CheckedException(msg.toString());
		}
	}

	public static void isBlank(String str, String message) {
		if (StrUtil.isBlank(str)) {
			throw new CheckedException(message);
		}
	}

	public static void isNull(Object object, String message) {
		if (object == null) {
			throw new CheckedException(message);
		}
	}

}
